package com.lowcode.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lowcode.platform.common.Result;
import com.lowcode.platform.entity.LowcodePage;
import com.lowcode.platform.service.LowcodePageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/lowcode")
public class LowcodePageController {
    
    @Autowired
    private LowcodePageService lowcodePageService;
    
    @PostMapping("/page")
    public Result<LowcodePage> createPage(@RequestBody LowcodePage page) {
        return Result.success(lowcodePageService.createPage(page));
    }
    
    @GetMapping("/page/{id}")
    public Result<LowcodePage> getPage(@PathVariable Long id) {
        return Result.success(lowcodePageService.getById(id));
    }
    
    @GetMapping("/page/list")
    public Result<IPage<LowcodePage>> listPages(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        Page<LowcodePage> page = new Page<>(pageNum, pageSize);
        return Result.success(lowcodePageService.page(page));
    }
    
    @PutMapping("/page/{id}")
    public Result<LowcodePage> updatePage(@PathVariable Long id, @RequestBody LowcodePage page) {
        page.setId(id);
        lowcodePageService.updateById(page);
        return Result.success(page);
    }
    
    @DeleteMapping("/page/{id}")
    public Result<Void> deletePage(@PathVariable Long id) {
        lowcodePageService.removeById(id);
        return Result.success();
    }
}
